//
//  UFOSXPPicPageViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFOSXPPicPageViewController.h"
#import "RGCardViewLayout.h"
#import "UFOPicDetailCollectionCell.h"
#import "UFOSXPPicPageListModel.h"
@interface UFOSXPPicPageViewController () <UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *picDetailCollectionV; // 三方collectionView
@property (nonatomic, retain) UIView *controlView;
@property (nonatomic, retain) UIAlertController *alertC;
@end

@implementation UFOSXPPicPageViewController

- (void)dealloc
{
    [_alertC release];
    [_controlView release];
    [_picDataArr release];
    [_picDetailCollectionV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createBackImageView];
    [self createCollectionView];
    [self createControlView];
}

// 控制视图
- (void)createControlView
{
    UITapGestureRecognizer *tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease];
    [self.view addGestureRecognizer:tapGR];
    
    self.controlView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREENHEIGHT, SCREENWIDTH, 60 * SCREENHEIGHT / HEIGHTSIXP)];
    self.controlView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.804];
    [self.view addSubview:self.controlView];
    [_controlView release];
    
    NSArray *imgArr = @[@"iconfont-fanhui-3.png", @"iconfont-xiazai.png", @"iconfont-shoucang-6.png", @"iconfont-share-3.png"];
    NSArray *textArr = @[@" 返回", @" 保存", @" 收藏", @" 分享"];
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(SCREENWIDTH / 4.0 * i, 0, SCREENWIDTH / 4.0, self.controlView.frame.size.height);
        button.tag = 10000 + i;
        [button setImage:[UIImage imageNamed:[imgArr objectAtIndex:i]] forState:UIControlStateNormal];
        [button setTitle:[textArr objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15.0 * SCREENHEIGHT / HEIGHTSIXP];
        button.backgroundColor = [UIColor clearColor];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.controlView addSubview:button];
    }
}

// 轻拍
- (void)tapAction:(UITapGestureRecognizer *)sender
{
    CGRect frame = self.controlView.frame;
    if (SCREENHEIGHT == frame.origin.y) {
        [UIView animateWithDuration:0.3 animations:^{
           
            CGRect frame = self.controlView.frame;
            frame.origin.y = SCREENHEIGHT - 60 * SCREENHEIGHT / HEIGHTSIXP;
            self.controlView.frame = frame;
            
        }];
    } else {
        [UIView animateWithDuration:0.3 animations:^{
            
            CGRect frame = self.controlView.frame;
            frame.origin.y = SCREENHEIGHT;
            self.controlView.frame = frame;
            
        }];
    }
}

// button方法
- (void)buttonAction:(UIButton *)sender
{
    if (10000 == sender.tag) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
    if (10001 == sender.tag) {
        NSInteger index = self.picDetailCollectionV.contentOffset.x / SCREENWIDTH;
        NSLog(@"%ld", index);
        UFOSXPPicPageListModel *model = [self.picDataArr objectAtIndex:index];
        NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:model.diy]];
        UIImage *image = [UIImage imageWithData:imgData];
        // 保存图片到系统相册
        UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
    }
}

// 判断是否成功保存到相册
- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
        self.alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"图片成功保存到本地相册" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:self.alertC animated:YES completion:^{
            [self performSelector:@selector(performAction) withObject:self afterDelay:1.5];
        }];
    }else
    {
        message = [error description];
    }
    NSLog(@"message is %@",message);
}

// 延迟
- (void)performAction
{
    if (self.alertC) {
        
        [self.alertC dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

// 背景
- (void)createBackImageView
{
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageV.image = [UIImage imageNamed:@"q=20120905_205823_618bf8407e819127_o.jpg"];
    imageV.userInteractionEnabled = YES;
    [self.view addSubview:imageV];
    [imageV release];
}

// 创建collectionView
- (void)createCollectionView
{
    RGCardViewLayout *layout = [[[RGCardViewLayout alloc] init] autorelease];
    
    self.picDetailCollectionV = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    self.picDetailCollectionV.backgroundColor = [UIColor clearColor];
    self.picDetailCollectionV.pagingEnabled = YES;
    self.picDetailCollectionV.delegate = self;
    self.picDetailCollectionV.dataSource = self;
    
    [self.view addSubview:self.picDetailCollectionV];
    [self.picDetailCollectionV release];
    
    [self.picDetailCollectionV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.view);
        
    }];
    
    [self.picDetailCollectionV registerClass:[UFOPicDetailCollectionCell class] forCellWithReuseIdentifier:@"picDetailCell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return  self.picDataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UFOPicDetailCollectionCell *cell = (UFOPicDetailCollectionCell  *)[collectionView dequeueReusableCellWithReuseIdentifier:@"picDetailCell" forIndexPath:indexPath];
    UFOSXPPicPageListModel *model = [self.picDataArr objectAtIndex:indexPath.section];
    [cell.imageV sd_setImageWithURL:[NSURL URLWithString:model.diy]];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
